﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Web.Mvc.Paging;

namespace Kooboo.Web.Areas.Commerce.Controllers
{
    using Kooboo.Commerce;
    using Kooboo.IoC;
    using Kooboo.Web.Mvc;
    using Kooboo.Commerce.Contexts.SupportTickets;
      
    
    public class SupportTicketController : ManagementController
    {
        #region Index
        
        public ActionResult Index(int? page, int? pagesize)
        {
           
            var query = this.Actor
                .QuerySupportTickets()
                .OrderByDescending(i => i.Id);

            ViewData["Pager"] = query.BuildPagerModel(this.Url.Action<SupportTicketController>(i => i.Index(null, null)), page??1, pagesize??Kooboo.Commerce.Configuration.PageSize);
                
            var skipCount = ((page??1) -1)*(pagesize??Kooboo.Commerce.Configuration.PageSize);
            var takeCount = pagesize ?? Kooboo.Commerce.Configuration.PageSize;

            ViewData["delete_action"] = this.Url.Action<SupportTicketController>(i => i.Delete(null));

            var viewUrl = this.Url.Action<SupportTicketController>(i => i.Edit(-1));
            

            var list = query.Skip(skipCount).Take(takeCount)
                .Select(i=> new SupportTicketViewModel
                {
                    Id = i.Id,
                    CreateDate = i.CreateDate,
                    Email = i.Email,
                    Subject = i.Subject,
                    EditUrl = viewUrl.Replace("-1",i.Id.ToString()),
                    Status = i.Status
                }); ;



            return View(list);
        }

        #endregion

        #region Edit
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var support = this.Actor
                .QuerySupportTickets()
                .Where(i => i.Id == id)
                .FirstOrDefault();

            ViewData["changestatus_action"] = this.Url.Action<SupportTicketController>(i=>i.ChangeStatus(id,UrlParameter<SupportStatus>.Empty));
            ViewData["reply_add_action"] = this.Url.Action<SupportTicketController>(i => i.AddReply(id, null));
            ViewData["index_action"] = this.Url.Action<SupportTicketController>(i => i.Index(null, null));
            ViewData["Status"] = support.Status.ToSelectableList();

            return View(support);
        }

        [HttpPost]
        public ActionResult ChangeStatus(int id, SupportStatus status)
        {
            var support = this.Actor
                .QuerySupportTickets()
                .Where(i => i.Id == id)
                .FirstOrDefault();

            if (support.Status != status)
            {
                support.Status = status;
                this.Actor.Put(support);
                
            }

            return Json(true);
        }

        [HttpPost]
        public ActionResult AddReply(int supportId, ISupportReply reply)
        {
            if (ModelState.IsValid)
            {
                var support = this.Actor
                    .QuerySupportTickets()
                    .Where(i => i.Id == supportId)
                    .FirstOrDefault();

                reply.IsAdminReply = true;
                reply.UserName = ContextContainer.Current.Resolve<Kooboo.Connect.User>().Name;
                reply.CreateDate = DateTime.Now;
                support.SupportReplies.Add(reply);
                this.Actor.Put(support);

                return Json(true);
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }

        #endregion

        [HttpPost]
        public ActionResult Delete(ISupportTicket item)
        {
            this.Actor.Delete(item);

            return Json(true);
        }

    
    }

 

}
